package model;

import context.*;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Point2D;
import java.awt.geom.Line2D;

import javax.media.j3d.Appearance;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.Material;
import javax.media.j3d.PointAttributes;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.TextureAttributes;
import javax.media.j3d.TransparencyAttributes;
import javax.vecmath.Color3f;

import com.sun.j3d.utils.image.TextureLoader;

public class Util implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG

  // UTILITIES

public static final Color veggFarge=new Color(137,113,72);
public static final Color gulvFarge=new Color(190,161,26);
public static final Color takFarge=new Color(130,137,141);
public static final Color rammeFarge=new Color(71,134,83);
//public static final Color rammeFarge=new Color(5,78,44);
public static final Color glassFarge=new Color(201,225,228);
public static final Color pipeFarge=Color.lightGray;
public static final Color pilarFarge=Color.lightGray;//darkGray;
//public static final Color uthusFarge=new Color(215,13,56);
public static final Color uthusFarge=new Color(165,7,25);

// ***************************************************************************
// *** UTILITY
// ***************************************************************************
private static java.net.URL textureImage=Resources.getResource("resources/images/stone.jpg");
//private static java.net.URL textureImage=Resources.getResource("resources/images/wood.jpg");
//private static java.net.URL textureImage=Resources.getResource("resources/images/texture.jpg");
//private static java.net.URL textureImage=Resources.getResource("resources/images/texture2.jpg");
//private static java.net.URL textureImage=Resources.getResource("resources/images/mur.jpg");
public static Appearance createAppearance(int idx,Color3f color)
{  Appearance app = new Appearance();

  // Globally used colors
  Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
  Color3f white = new Color3f(1.0f, 1.0f, 1.0f);

  switch (idx)
  {
    case 0: // Unlit solid
      {  // Set up the coloring properties
        ColoringAttributes ca = new ColoringAttributes();
        ca.setColor(color);
        app.setColoringAttributes(ca);
        break;
      }
    case 1: // Unlit wire frame
      {  // Set up the coloring properties
        ColoringAttributes ca = new ColoringAttributes();
        ca.setColor(color);
        app.setColoringAttributes(ca);
        // Set up the polygon attributes
        PolygonAttributes pa = new PolygonAttributes();
        pa.setPolygonMode(PolygonAttributes.POLYGON_LINE);
        pa.setCullFace(PolygonAttributes.CULL_NONE);
        app.setPolygonAttributes(pa);
        break;
      }
    case 2: // Unlit points
      {  // Set up the coloring properties
        ColoringAttributes ca = new ColoringAttributes();
        ca.setColor(color);
        app.setColoringAttributes(ca);
        // Set up the polygon attributes
        PolygonAttributes pa = new PolygonAttributes();
        pa.setPolygonMode(PolygonAttributes.POLYGON_POINT);
        pa.setCullFace(PolygonAttributes.CULL_NONE);
        app.setPolygonAttributes(pa);
        // Set up point attributes
        PointAttributes pta = new PointAttributes();
        pta.setPointSize(5.0f);
        app.setPointAttributes(pta);
        break;
      }

    case 3: // Lit solid
      {  // Set up the material properties
        app.setMaterial(new Material(color,black,color,white,80.0f));
        break;
      }
    case 4: // Texture mapped, lit solid
      {  // Set up the texture map
        TextureLoader tex = new TextureLoader(textureImage,null);
        app.setTexture(tex.getTexture());
        TextureAttributes texAttr = new TextureAttributes();
         texAttr.setTextureMode(TextureAttributes.MODULATE);
         app.setTextureAttributes(texAttr);
        // Set up the material properties
        app.setMaterial(new Material(white, black, white, black, 1.0f));
//        app.setMaterial(new Material(color,black,color,black,80.0f));
        break;
      }
    case 5: // Transparent, lit solid
      {  // Set up the transparency properties
        TransparencyAttributes ta = new TransparencyAttributes();
        ta.setTransparencyMode(TransparencyAttributes.BLENDED);
        ta.setTransparency(0.6f);
        app.setTransparencyAttributes(ta);
        // Set up the polygon attributes
        PolygonAttributes pa = new PolygonAttributes();
        pa.setCullFace(PolygonAttributes.CULL_NONE);
        app.setPolygonAttributes(pa);
        // Set up the material properties
        app.setMaterial(new Material(color,black,color,black,1.0f));
        break;
      }

    case 6: // Lit solid, no specular
      {  // Set up the material properties
        app.setMaterial(new Material(color,black,color,black,80.0f));
        break;
      }

    case 7: // Lit solid, specular only
      {  // Set up the material properties
//        Color3f objColor = new Color3f(0.8f, 0.0f, 0.0f);
//        app.setMaterial(new Material(black,black,black,white,80.0f));
        app.setMaterial(new Material(color,white,color,black,1.0f));
//        app.setMaterial(new Material());
        break;
      }

    case 8: // Another lit solid with a different color
      {  // Set up the material properties
        app.setMaterial(new Material(color,black,color,white,80.0f));
        break;
      }
    default:
      {  ColoringAttributes ca = new ColoringAttributes();
        ca.setColor(new Color3f(0.0f, 1.0f, 0.0f));
        app.setColoringAttributes(ca);
      }
  }

  return app;
}

  // ******************************************************************
  // *** UTILITIES: NYE !!!!
  // ******************************************************************



  public static void draw(Graphics2D g,Shape s)
  { g.draw(s); }

  public static void fill(Graphics2D g,Shape s,Color c)
  { if(c!=null)
    { Color color=g.getColor();
      g.setColor(c);
      g.fill(s);
      g.setColor(color);
    }
  }

  public static void drawLine(Graphics2D g,Point2D.Double p1,Point2D.Double p2)
  { drawLine(g,p1.x,p1.y,p2.x,p2.y); }

//  public static void drawLine(Graphics2D g,double x1,double y1,double x2,double y2)
  public static void drawLine(Graphics2D g,double x1,double y1,double x2,double y2)
  {
//    g.drawLine((int)x1,(int)y1,(int)x2,(int)y2);
    Line2D.Double line=new Line2D.Double(x1,y1,x2,y2);
    g.draw(line);
  }

  public static void drawRect(Graphics2D g,double x,double y,double width,double height)
  { draw(g,new Rectangle2D.Double(x,y,width,height)); }

  public static void fillRect(Graphics2D g,double x,double y,double width,double height,Color c)
  { fill(g,new Rectangle2D.Double(x,y,width,height),c); }
//
//  protected void drawOval(Graphics2D g,double x,double y,double width,double height)
//  { g.drawOval((int)x,(int)y,(int)width,(int)height); }
//
//  protected void fillOval(Graphics2D g,double x,double y,double width,double height,Color c)
//  { Color color=g.getColor();
//    g.setColor(c);
//    g.fillOval((int)x,(int)y,(int)width,(int)height);
//    g.setColor(color);
//  }

  public static void drawArc(Graphics2D g,double x,double y,double width,double height,double startAngle,double arcAngle)
  { g.drawArc((int)x,(int)y,(int)width,(int)height,(int)startAngle,(int)arcAngle); }

  public static void drawString(Graphics2D g,String str,double x,double y)
  { g.drawString(str,(int)x,(int)y); }

//  public static void label(Graphics2D g,String str,double x,double y)
  public static void label(Graphics g,String str,double x,double y)
  { Font font=g.getFont();
    Rectangle2D box=font.getStringBounds(str,new java.awt.font.FontRenderContext(null,false,false));
    double width=box.getWidth()+8;
    double height=box.getHeight();
    fillRect((Graphics2D)g,x-width/2,y-height/2-1,width,height,Color.white);
//    fillRect(g,x-width/2,y-height/2,width,height,Color.yellow);
    drawString((Graphics2D)g,str,x-width/2+4,y+height/2-4);
  }

}
